package test;

import bean.Users;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;


public class Demo {
    public static void main(String[] args) {
        //java->json

        Users users = new Users(11,"aaa","123456",123.456);
        JSONObject jsonObject = JSONObject.fromObject(users);
        System.out.println(jsonObject);//json

        //json-java
        String str="{\"money\":123.456,\"password\":\"123456\",\"uid\":11,\"username\":\"aaa\"}";
        JSONObject jsonObject1 = JSONObject.fromObject(str);
        Object o = JSONObject.toBean(jsonObject1, Users.class);
        System.out.println("o="+o);
        //java集合->json
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Users(1,"张三","123",12.34));
        arrayList.add(new Users(2,"张三2","12322",1244.34));
        arrayList.add(new Users(3,"张三3","12333",1255.34));
        JSONArray jsonArray = JSONArray.fromObject(arrayList);//json
        System.out.println(jsonArray);
        //json->java集合
        String str2="[{\"money\":12.34,\"password\":\"123\",\"uid\":1,\"username\":\"张三\"},{\"money\":1244.34,\"password\":\"12322\",\"uid\":2,\"username\":\"张三2\"},{\"money\":1255.34,\"password\":\"12333\",\"uid\":3,\"username\":\"张三3\"}]";
        JSONArray jsonArray2 = JSONArray.fromObject(str2);

        Object[] o1 = (Object[])JSONArray.toArray(jsonArray2, Users.class);
        for (Object o2 : o1) {
            System.out.println("o2="+o2);
        }

    }
}
